﻿using NetFwTypeLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddFirewall
{
    class Program
    {
        /// <param name="args[0]">名称，如有空格需使用英文双引号包裹</param>
        /// <param name="args[1]">端口</param>
        /// <param name="args[2]">协议(TCP、UDP、ALL)</param>
        /// <param name="args[3]">跳过确认，1：是，0：否，默认：0</param>
        /// <param name="args[4]">自动关闭，1：是，0：否，默认：0</param>
        static void Main(string[] args)
        {
            string name = (null != args && args.Length > 0) ? args[0] : "";
            string port = (null != args && args.Length > 1) ? args[1] : "";
            string protocol = ((null != args && args.Length > 2) ? args[2] : "").ToUpper();
            string skipEnter = (null != args && args.Length > 3) ? args[3] : "0";
            string autoClose = (null != args && args.Length > 4) ? args[4] : "0";

            name = readName(name);
            port = readPort(port);
            protocol = readProtocol(protocol);

            Console.WriteLine();
            Console.WriteLine(string.Format("当前添加的规则为：\r\n规则名称：{0}\r\n端口号：{1}\r\n协议类型：{2}", name, port, ("ALL".Equals(protocol) ? "TCP、UDP" : protocol)));

            if (!"1".Equals(skipEnter))
            {
                Console.WriteLine();
                Console.WriteLine("确认请输入Y，输入其它字符则取消，不区分大小写，回车输入：");
                string enter = Console.ReadLine().ToUpper();
                if (!"Y".Equals(enter))
                {
                    Console.WriteLine();
                    Console.WriteLine("添加防火墙规则失败，用户已取消");

                    if (!"1".Equals(autoClose))
                    {
                        Console.ReadKey();
                    }
                    return;
                }
            }

            if ("ALL".Equals(protocol))
            {
                //    INetFwManger.NetFwAddPorts(name, p, "TCP");
                //    INetFwManger.NetFwAddPorts(name, p, "UDP");
                INetFwManger.CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, name, port);
                INetFwManger.CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP, name, port);
            }
            else if ("TCP".Equals(protocol))
            {
                INetFwManger.CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, name, port);
            }
            else if ("UDP".Equals(protocol))
            {
                INetFwManger.CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP, name, port);
            }
            Console.WriteLine();
            Console.WriteLine("防火墙规则已添加");

            if (!"1".Equals(autoClose))
            {
                Console.ReadKey();
            }
        }

        static string readName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                Console.WriteLine("请输入协议名称，回车输入：");
                name = Console.ReadLine();
                return readName(name);
            }

            return name;
        }


        static string readPort(string port)
        {
            int p = 0;
            if (string.IsNullOrEmpty(port)
                || !int.TryParse(port, out p)
                || p <= 0 || p > 65535)
            {
                Console.WriteLine("请输入协议端口号(0-65535，不含0)，正整数，回车输入：");
                port = Console.ReadLine();
                return readPort(port);
            }

            return port;
        }

        static string readProtocol(string protocol)
        {
            if (string.IsNullOrEmpty(protocol)
                || (!"TCP".Equals(protocol)
                && !"UDP".Equals(protocol)
                && !"ALL".Equals(protocol)))
            {
                Console.WriteLine("请输入协议类型(TCP、UDP、ALL)，不区分大小写，回车输入：");
                protocol = Console.ReadLine().ToUpper();
                return readProtocol(protocol);
            }

            return protocol;
        }

    }
}
